This step-by-step tutorial will guide you to build a complete application that analyzes a Code 39 barcode from an image and outputs the value, type, location and confidence to the terminal.
Before running this tutorial, a license for Barcode Xpress for Java should already be installed.
This demo works both on Windows and Linux platforms.
Copy Code
|
|
---|---|
import com.accusoft.barcodexpress.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class BxJavaDemo { public static void main(String[] args) { … } } |
Copy Code
|
|
---|---|
BarcodeXpress barcodeXpress = new BarcodeXpress(); BarcodeReader barcodeReader = barcodeXpress.getReader(); // The BarcodeXpress.setSolutionName, BarcodeXpress.setSolutionKey and possibly the BarcodeXpress.setOemLicenseKey methods must be // called to distribute the runtime. Note that the SolutionName, SolutionKey and OEMLicenseKey values shown below are only for example. // BarcodeXpress.setSolutionName("YourSolutionName"); // BarcodeXpress.setSolutionKey(1234,1234,1234,1234); // BarcodeXpress.setOemLicenseKey("2.0.YourOEMLicenseKeyGoesHere"); |
Copy Code
|
|
---|---|
if (0 == args.length) { System.out.println("Please specify input image(s)."); return; } String inputFilePath = args[0]; |
Copy Code
|
|
---|---|
BarcodeType barcodeType = BarcodeType.CODE39;
barcodeReader.setBarcodeTypes(new BarcodeType[] { BarcodeType.CODE39});
|
Copy Code
|
|
---|---|
System.out.println("Scanning file '" + inputFilePath + "' for " + barcodeType.toString() + " barcodes.\n"); |
Copy Code
|
|
---|---|
try { … } catch (BarcodeException bEx) { System.out.println(bEx.getMessage()); } catch (Exception ex) { System.out.println(ex.getMessage()); } |
Copy Code
|
|
---|---|
File inputFile = new File(inputFilePath);
BufferedImage bufferedImage = ImageIO.read(inputFile);
|
Copy Code
|
|
---|---|
Result[] results = barcodeReader.analyze(bufferedImage); System.out.println(results.length + " barcodes found. \n"); for (int i = 0; i < results.length; i++) { System.out.println("#" + (i+1)); System.out.println("Barcode value = '" + results[i].getValue() + "'"); System.out.println("Barcode type = '" + results[i].getType() + "'\n"); } |
Copy Code
|
|
---|---|
import com.accusoft.barcodexpress.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class BxJavaDemo { public static void main(String[] args) { BarcodeXpress barcodeXpress = new BarcodeXpress(); BarcodeReader barcodeReader = barcodeXpress.getReader(); // The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey method must be // called to distribute the runtime. Note that the SolutionName, SolutionKey and // OEMLicenseKey values shown below are only examples. // BarcodeXpress.setSolutionName("YourSolutionName"); // BarcodeXpress.setSolutionKey(1234,1234,1234,1234); // BarcodeXpress.setOemLicenseKey("2.0.YourOEMLicenseKeyGoesHere"); if (0 == args.length) { System.out.println("Please specify input image(s)."); return; } String inputFilePath = args[0]; BarcodeType barcodeType = BarcodeType.CODE39; barcodeReader.setBarcodeTypes(new BarcodeType[] { barcodeType }); System.out.println("Scanning file '" + inputFilePath + "' for " + barcodeType.toString() + " barcodes.\n"); try { File inputFile = new File(inputFilePath); BufferedImage bufferedImage = ImageIO.read(inputFile); Result[] results = barcodeReader.analyze(bufferedImage); System.out.println(results.length + " barcodes found. \n"); for (int i = 0; i < results.length; i++) { System.out.println("#" + (i+1)); System.out.println("Barcode value = '" + results[i].getValue() + "'"); System.out.println("Barcode type = '" + results[i].getType() + "'\n"); } } catch (BarcodeException bEx) { System.out.println(bEx.getMessage()); } catch (Exception ex) { System.out.println(ex.getMessage()); } } } |
Linux |
Copy Code
|
---|---|
javac -cp ".:src/main/java:../../bin/barcodexpressjava-13.1.jar" src/main/java/BxJavaDemo.java
|
Windows |
Copy Code
|
---|---|
javac -cp ".;src/main/java;../../bin/barcodexpressjava-13.1.jar" src/main/java/BxJavaDemo.java
|
Linux |
Copy Code
|
---|---|
java -cp ".:src/main/java:../../bin/barcodexpressjava-13.1.jar" BxJavaDemo ../images/Barcode-Realworld-Code39-3.bmp
|
Windows |
Copy Code
|
---|---|
java -cp ".;src/main/java;../../bin/barcodexpressjava-13.1.jar" BxJavaDemo ../images/Barcode-Realworld-Code39-3.bmp
|
Copy Code
|
|
---|---|
Scanning file '../images/Barcode-Realworld-Code39-3.bmp' for CODE39 barcodes. 2 barcodes found. #1 Barcode value = '1202804105' Barcode type = 'CODE39' #2 Barcode value = '95905568' Barcode type = 'CODE39' |
Copy Code
|
|
---|---|
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.accusoft.barcodexpress.samples</groupId> <artifactId>BxJavaDemo</artifactId> <version>1.0</version> <repositories> <repository> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>false</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> <id>accusoft-public-releases</id> <name>Accusoft Maven Releases</name> <url>http://mvn.accusoft.com</url> <layout>default</layout> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.accusoft.barcodexpress</groupId> <artifactId>barcodexpressjava</artifactId> <version>13.1</version> </dependency> </dependencies> </project> |
Copy Code
|
|
---|---|
mvn clean package dependency:copy-dependencies |
Linux |
Copy Code
|
---|---|
java -cp "target/BxJavaDemo-1.0.jar:target/dependency/barcodexpressjava-13.1.jar" BxJavaDemo ../images/Barcode-Realworld-Code39-3.bmp
|
Windows |
Copy Code
|
---|---|
java -cp "target/BxJavaDemo-1.0.jar;target/dependency/barcodexpressjava-13.1.jar" BxJavaDemo ../images/Barcode-Realworld-Code39-3.bmp
|